﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using prismapp.Views;

namespace prismapp.ViewModels
{
    internal class MainViewModel : BindableBase
    {
        public DelegateCommand<string> OpenCommand { get; set; }
        public DelegateCommand? BackCommand { get; }
        public DelegateCommand<string> OpenWindowsCommand { get; }

        public IRegionNavigationJournal? NavigationJournal { get; set; }

        private readonly IRegionManager _regionManager;
        private readonly IDialogService _dialogService;

        public MainViewModel(IRegionManager regionManager,IDialogService dialogService)
        {
            OpenCommand = new DelegateCommand<string>(Open);
            this._regionManager = regionManager;
            BackCommand = new DelegateCommand(Back);
            OpenWindowsCommand = new DelegateCommand<string>(OpenWindows);
            _dialogService = dialogService;
            
        }

        private void OpenWindows(string obj)
        {
            var keys = new DialogParameters { { "Value", "Hello,World" } };
            _dialogService.ShowDialog(obj,keys,
                callback =>
                {
                    var res = callback.Parameters.GetValue<string>("Value");
                });

        }

        private void Back()
        {
            var regionNavigationJournal = this.NavigationJournal;
            if (regionNavigationJournal is { CanGoBack: true })
            {
                this.NavigationJournal?.GoBack();
            }
        }

        private void Open(string obj)
        {
            var key = new NavigationParameters { { "Title", "hello" } };
            _regionManager.Regions[regionName: "ContentRegion"].RequestNavigate(obj,
                callback =>
                {
                    if ((bool)callback.Result!)
                    {
                        this.NavigationJournal = callback.Context.NavigationService.Journal;
                    }
                },
                key
            );
        }
    }
}